package com.sihan.framework.etm.service.impl;

import com.framework.mybatis.Condition;
import com.framework.mybatis.service.impl.BaseServiceImpl;
import com.sihan.framework.etm.entity.SecurityParam;
import com.sihan.framework.etm.entity.User;
import com.sihan.framework.etm.mapper.SecurityParamMapper;
import com.sihan.framework.etm.mapper.UserMapper;
import com.sihan.framework.etm.service.SecurityParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;

/**
 * @author QinC
 * @create 2018-09-06 14:48
 */
@Service
public class SecurityParamServiceImpl extends BaseServiceImpl<SecurityParam, Long> implements SecurityParamService {

    @Autowired
    private SecurityParamMapper securityParamMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public SecurityParam getSecurity() {
        return securityParamMapper.selectOne(new Condition<>(new SecurityParam(1)));
    }

    @Transactional
    @Override
    public void updateSecurity(SecurityParam sp) {
        SecurityParam oldSp = getSecurity();
        boolean su = securityParamMapper.updateById(sp);

        if(su) {
            //更新任务执行时间
            Calendar calendar = Calendar.getInstance();
            if(oldSp.getTempAccount().intValue() != sp.getTempAccount().intValue()) {
                //1.初始化临时账号（激活账号，非转激活）
                @SuppressWarnings("unchecked")
				List<User> tempUsers = userMapper.selectList(new Condition<>().eq("user_type", 1)
                        .eq("state", 0).eq("has_active", false));

                for (User sysUsers : tempUsers) {
                    calendar.setTime(sysUsers.getCreateTime());
                    calendar.add(Calendar.MONTH, sp.getTempAccount());
                }
            }

            if(oldSp.getSleepAccount().intValue() != sp.getSleepAccount().intValue()) {
                //2.初始化休眠期账号（休眠账号）
                @SuppressWarnings("unchecked")
				List<User> sleepUsers = userMapper.selectList(new Condition<>().eq("state", 1));
                for (User sysUsers : sleepUsers) {
                    calendar.setTime(sysUsers.getSleepTime());
                    calendar.add(Calendar.MONTH, sp.getSleepAccount());
                    calendar.add(Calendar.SECOND, -7*24*3600);
                }
            }

            if(oldSp.getLockTime().intValue() != sp.getLockTime().intValue()) {
                //4.初始化锁定账号任务（只查询已锁定账号）
                @SuppressWarnings("unchecked")
				List<User> lockUsers = userMapper.selectList(new Condition<>().eq("locked", true));
                for (User sysUsers : lockUsers) {
                    calendar.setTime(sysUsers.getLockTime());
                    calendar.add(Calendar.MINUTE, sp.getLockTime());
                }
            }

            if(oldSp.getScanCycle().intValue() != sp.getScanCycle().intValue()) {
                //3.初始化定期扫描账号（只扫描激活状态的用户，其他无意义）
            }
        }
    }



}
